<template>
  <div class="mobile-search">
    <!-- 桌面端显示内联表单 -->
    <el-form v-if="!isMobile" :model="searchForm" inline class="search-form">
      <slot :search-form="searchForm"></slot>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    
    <!-- 移动端显示可折叠的搜索 -->
    <div v-else class="mobile-search-container">
      <div class="search-header">
        <el-button 
          type="primary" 
          :icon="Search" 
          @click="showSearch = !showSearch"
          class="search-toggle"
        >
          {{ showSearch ? '收起搜索' : '展开搜索' }}
        </el-button>
      </div>
      
      <el-collapse-transition>
        <div v-show="showSearch" class="search-content">
          <el-form :model="searchForm" class="mobile-search-form">
            <slot :search-form="searchForm"></slot>
            <div class="search-actions">
              <el-button type="primary" @click="handleSearch" class="search-btn">
                搜索
              </el-button>
              <el-button @click="handleReset" class="reset-btn">
                重置
              </el-button>
            </div>
          </el-form>
        </div>
      </el-collapse-transition>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { useAppStore } from '@/stores/app'

const props = defineProps({
  searchForm: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['search', 'reset'])

const appStore = useAppStore()
const isMobile = computed(() => appStore.device === 'mobile')
const showSearch = ref(false)

const handleSearch = () => {
  emit('search')
  if (isMobile.value) {
    showSearch.value = false
  }
}

const handleReset = () => {
  emit('reset')
  if (isMobile.value) {
    showSearch.value = false
  }
}
</script>

<style lang="scss" scoped>
.mobile-search {
  margin-bottom: 20px;
}

.search-form {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 0;
}

.mobile-search-container {
  .search-header {
    margin-bottom: 12px;
    
    .search-toggle {
      width: 100%;
      height: 44px;
      font-size: 16px;
    }
  }
  
  .search-content {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 16px;
    
    .mobile-search-form {
      :deep(.el-form-item) {
        margin-bottom: 16px;
        
        .el-form-item__label {
          display: block;
          margin-bottom: 8px;
          font-weight: 500;
          color: #333;
        }
        
        .el-form-item__content {
          width: 100%;
          
          .el-input,
          .el-select {
            width: 100%;
          }
          
          .el-input__inner,
          .el-select__wrapper {
            height: 44px;
            font-size: 16px;
          }
        }
      }
    }
    
    .search-actions {
      display: flex;
      gap: 12px;
      margin-top: 20px;
      
      .search-btn,
      .reset-btn {
        flex: 1;
        height: 44px;
        font-size: 16px;
      }
    }
  }
}
</style>
